package client.gui;



import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


import messages.MessageLogin;
import messages.MessageLoginReply;
import messages.MessageLoginReply.ErrMsg;



import sharedspace.*;



/**
 * This is the Login Screen .
 * @author Amir S.
 * @author Jacob C.
 * @author Maya G.
 * @author Michael V.
 *
 */
public class GUI_Login extends JPanel {


    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private User user = null;
	
	boolean emptyUNflag=false;
	boolean emptyPWflag=false;
	
	private String currentUsername = null; 
	
	private GUI_ImagePanel background;


	public JButton Close = null;
	public Client_GUI ms=null;
	private JButton Login = null;
	private JTextField Username = null;
	private JLabel User = null;
	private JLabel Pass = null;
	private JButton Exit = null;
	private JPasswordField jPasswordField = null;
	private JLabel jLabel = null;
	/**
	 * This is the default constructor
	 * @param MS
	 */
	public GUI_Login(Client_GUI MS) {
		super();
		this.ms = MS;
		initialize();
	}

	/**
	 * This method initializes the Login screen
	 */
	private void initialize() {
		
		
		
		jLabel = new JLabel();
		jLabel.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 50));
		jLabel.setBounds(new Rectangle(384, 29, 212, 57));
		jLabel.setForeground(Color.BLUE);
		jLabel.setText("Login");
		this.setSize(1000, 700);
		Pass = new JLabel();
		Pass.setSize(new Dimension(141, 31));
		Pass.setLocation(new Point(306, 318));
		Pass.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 25));
		Pass.setForeground(Color.BLUE);
		Pass.setText("Password:");
		
		User = new JLabel();
		User.setSize(new Dimension(136, 37));
		User.setLocation(new Point(309, 258));
		User.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 25));
		User.setForeground(Color.BLUE);
		User.setText("Username:");
		
		this.setLayout(null);
		this.add(getLogin(), null);
		this.add(getUsername(), null);
		this.add(User, null);
		this.add(Pass, null);
		this.add(getExit(), null);
		this.add(getJPasswordField(), null);
		this.add(jLabel, null);
		background = new GUI_ImagePanel(new ImageIcon("images/background1.jpg").getImage());
		this.add(background);
	

	}
	
	/**
	 * This method initializes Username text field	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	public JTextField getUsername() {
		if (Username == null) {
			Username = new JTextField();
			Username.setPreferredSize(new Dimension(5, 25));
			Username.setSize(new Dimension(200, 38));
			Username.setFont(new Font("Arial", Font.BOLD, 25));
			Username.setLocation(new Point(465, 257));
		}
		return Username;
	}

	

	/**
	 * This method initializes Login	
	 * 	sends MessageLogin and receives MessageLoginReply messages .
	 * @return javax.swing.JButton	
	 */
	public JButton getLogin() {
		if (Login == null) {
			
			//Set Graphics
			Login = new JButton();
			Login.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 20));
			Login.setBounds(new Rectangle(515, 482, 146, 34));
			Login.setText("Login");
			
			//Button "Login" pressed
			Login.addActionListener(new java.awt.event.ActionListener() {
				
			
				@SuppressWarnings("deprecation")
				public void actionPerformed(java.awt.event.ActionEvent e) {
								
					//Creating new login message and inserting login information to it
					user = new User();
					user.setUserID(Username.getText());
					user.setUserPassword(jPasswordField.getText());
					
					emptyUNflag=false;
					
					if(user.getUserID().equals(""))
					{
						JOptionPane.showMessageDialog(null, "Please insert username", "NOTICE", JOptionPane.INFORMATION_MESSAGE);
						User.setForeground(Color.RED);
						emptyUNflag=true;
					}
					
					
					
					emptyPWflag=false;
					
					if(user.getUserPassword().equals(""))
					{
						
						JOptionPane.showMessageDialog(null, "Please insert password", "NOTICE", JOptionPane.INFORMATION_MESSAGE);
						Pass.setForeground(Color.RED);
						emptyPWflag=true;
					}
					
					else { /*Pass.setForeground(Color.WHITE);*/	}
					
				
					
					if(!emptyPWflag && !emptyUNflag) 
					{
						MessageLogin msgLog=new MessageLogin(user);	
									
						try {
							//Sending the login message to server
							
							ms.client.sendToServer(msgLog);	

							
	
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} 
						
						//Getting reply to login from client
						MessageLoginReply msglogrep = (MessageLoginReply) ms.client.GetMessage();         
	                    
	                    //Login successful scenarios
	                    if(msglogrep.getError()==ErrMsg.INCORRECT)
                    		JOptionPane.showMessageDialog(null,msglogrep.GetErrorMessage(1));
	                    if(msglogrep.getError()== ErrMsg.SUCCESS) 
	                    {
	                    	
	                    	user=msglogrep.getUser();
	                    	currentUsername =  Username.getText();
							switch(msglogrep.getUser().getUserPermission()) {
							
							case 0: //user is GUEST
							{
								
								try {
									
									jPasswordField.setText(null);
		                    		ms.gst = new GUI_Guest(ms);
		                    		ms.setContentPane(ms.gst);
								} catch (Exception e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
							}
							break;
							case 1: //user is STUDENT
							{
								
								try {
									//JOptionPane.showMessageDialog(null,"user is a student");
									Student student = new Student(user);
									ms.stdnt = new GUI_Student(ms, student);
									ms.setContentPane(ms.stdnt);
								} catch (Exception e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
							}
							break;
							case 2: //user is TEACHER
							{
								
								try {
									ms.tchr = new GUI_Teacher(ms,user);
									ms.setContentPane(ms.tchr);
								} catch (Exception e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
							}
							break;
							case 3: //user is MANAGER
							{
								
								try {
									ms.mngr = new GUI_Manager(ms);
									ms.setContentPane(ms.mngr);
								} catch (Exception e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
							}
							break;
							case 4: //user is ADMIN
							{
								
								try {
									ms.admn = new GUI_Admin(ms);
									ms.setContentPane(ms.admn);
								} catch (Exception e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
							}
							
							break;							
	                    }	//end switch
	                   } //end if logged in
	                    
	                    if(msglogrep.getError() == ErrMsg.ALREADY_LOGIN)
	                    {
	                    	try {
	                    		if (msglogrep.getUser().getUserPermission() == 4){
									ms.admn = new GUI_Admin(ms);
									ms.setContentPane(ms.admn);
	                    		}
	                    		else
								JOptionPane.showMessageDialog(null,"user is already logged - access denied");
							} catch (Exception e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
	                    }
	                    //Login failed scenarios
	                    if(msglogrep.getError()== ErrMsg.UNKNOWN)
	                    {
	                    	try{
	                    	JOptionPane.showMessageDialog(null,msglogrep.GetErrorMessage(5));
	                    	}
	                    	catch (Exception e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
	                    }
          
					}
				}
			});
		}
		return Login;
	}

	/**
	 * This method initializes Exit	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getExit() {
		if (Exit == null) {
			Exit = new JButton();
			Exit.setText("Exit");
			Exit.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 20));
			Exit.setBounds(new Rectangle(305, 482, 146, 34));
			Exit.setActionCommand("Exit");
			Exit.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					int i = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit AES System?", "AES System", JOptionPane.YES_NO_OPTION);
					if (i == JOptionPane.YES_OPTION)
					{
						JOptionPane.showMessageDialog(null,"Thanks For Using AES System");
						System.exit(ABORT);
					}
				}
			});
		}
		return Exit;
	}

	/**
	 * This method initializes Password	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	public JPasswordField getJPasswordField() {
		if (jPasswordField == null) {
			jPasswordField = new JPasswordField();
			jPasswordField.setLocation(new Point(465, 315));
			jPasswordField.setFont(new Font("Arial", Font.BOLD, 14));
			jPasswordField.setSize(new Dimension(200, 35));
		}
		return jPasswordField;
	}
	
	public String getCurrentUsername(){
		return user.getUserFirstName() + " " + user.getUserLastName();
	}
	
	public String getCurrentUserID(){
		return user.getUserID();
	}
	/*public String getCurrentPermission() {
		return currPermission;
	}*/
	
	public User getUser(){
		return user;
	}
}  
